<?php

/**
 * 封装投票项目数据缓存
 * Enter description here ...
 * @author codetaste
 *
 */

class PayForAuction{
	
	private $User;
	
	private $Auction = 0;
	
	public function __construct($user){
		$this->User = $user;
		$this->Auction = getInfoById('PrizeAuction', $this->User->u_lock_auid);
	}

	public function pay(){
		$_result = $this->checkUser();
		if ($_result == 1)
			$_result = $this->checkUserLock();
		if ($_result == 1)
			$_result = $this->checkLockTime ();
		if ($_result == 1)
			$_result = $this->realPay ();
//		dump($_result);
//		exit;
	}
	
	private function checkUser(){
		return $this->User->u_id>0 ? 1 : -1 ;
	}
	
	private function checkUserLock(){
		return $this->User->u_lock_auid>0 ? 1 : -2 ;
	}
	
	private function checkLockTime(){
		return time()>$this->Auction['au_etime'] ? 1 : -3 ;
	}
	
	private function realPay(){
		# 扣除金币
		import('@.Util.UserGolds');
		$card_params = array('reg_gold'=> -$this->Auction['au_golds'], 'reg_description'=>'拍卖奖品:'.$this->Auction['au_name'].',扣去金币:'.$this->Auction['au_golds']);
		
		$card_userGolds = new UserGolds($this->User, $card_params);
		$card_userGolds->save();
		
		# 解锁
		$_u['u_lock_auid'] = 0;
		$_con['u_id'] = $this->User->u_id;
		M('User')->where($_con)->data($_u)->save();
		# 获取奖品
		$_rep['rep_type'] = 'auction';
		$_rep['u_id'] = $this->User->u_id;
		$_rep['prst_id'] = $this->Auction['prst_id'];
		$_rep['rep_time '] = time();
		$_rep['rep_description'] = '成功拍卖:'.$this->Auction['au_name'].'，获得奖品:'.$this->Auction['prst_id'];
		M('RecordPrize')->data($_rep)->add();
		
	}
	
	
	
	
}

